home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / rlib / trace < prev    next >
Text File  |  1994-09-23  |  524b  |  27 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    trace ( A )
  4.  
  5. //  Description:
  6.  
  7. //  Compute the trace (the sum of the diagonal elements) for the input
  8. //  matrix. `trace(A)' is the same as `sum( diag( A ) )'.
  9.  
  10. //-------------------------------------------------------------------//
  11.  
  12. trace = function(m) 
  13. {
  14.   if(m.class != "num") 
  15.   { 
  16.     error("must provide NUMERICAL input to trace()");
  17.   }
  18.  
  19.   tr = 0;
  20.   for(i in 1:min( [m.nr, m.nc] )) 
  21.   {
  22.     tr = tr + m[i;i];
  23.   }
  24.   
  25.   return tr;
  26. };
  27.